<template>
 <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="单位类型" prop="mainType">
        <el-select v-model="queryParams.mainType" placeholder="请选择单位类型" clearable style="width: 240px">
          <el-option label="请选择字典生成" value="" />
        </el-select>


      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select
            v-model="queryParams.status"
            placeholder="审核状态"
            clearable
            style="width: 240px"
        >
            <el-option
              v-for="dict in filling_check_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
        </el-select>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Document"
          @click="batDownload"
          v-hasPermi="['finFilling:main:batDownload']"
        >批量下载报告</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Warning"
          @click="sensitiveWord"
          v-hasPermi="['finFilling:main:sensitiveWord']"
        >敏感词</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Link"
          @click="downLink"
          v-hasPermi="['finFilling:main:downLink']"
        >下载填报链接</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="CircleCheck"
          @click="oneClickReview"
          v-hasPermi="['finFilling:main:oneClickReview']"
        >一键审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="CircleClose"
          @click="oneClickCancelReview"
          v-hasPermi="['finFilling:main:oneClickCancelReview']"
        >一键取消审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Download"
          @click="downPrefCondition"
          v-hasPermi="['finFilling:main:downPrefCondition']"
        >下载绩效上传情况</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection"  align="center" />
      <el-table-column label="单位名称"  align="center" prop="compname" />
      <el-table-column label="所属县市"  align="center" prop="department" />
      <el-table-column label="科室" align="center" prop="leader" width="115"/>
      <el-table-column label="是否上传绩效表" align="center"  prop="sjjz" />
      <el-table-column label="上年数据" align= "center" >
          <template #default="{ row }">
              <span :style="{ 
                  color: row.yearupfile_guid == null ? '#f62039' : '#67C23A'
              }">
                  {{ row.yearupfile_guid == null ? '未上传' :  '已上传' }}
              </span>
          </template>
      </el-table-column>
      <el-table-column label="本年数据" align= "center">
        <template #default="{ row }">
              <span :style="{
                  color: row.yearnowfile_guid == null ? '#f62039' : '#67C23A'
              }">
                  {{ row.yearnowfile_guid == null ? '未上传' :  '已上传' }}
              </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="view" @click="editMain(scope.row)" v-hasPermi="['FinFilling:main:editMain']">查看</el-button>
          <el-button link type="primary" icon="memo" @click="showPref(scope.row)" v-hasPermi="['FinFilling:main:showPref']">整体绩效表</el-button>
          <el-button link type="primary" icon="download" @click="downZip(scope.row)" v-hasPermi="['FinFilling:main:downZip']">下载公开报告</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script setup name="Main">
import { listMain, getMain, delMain, addMain, updateMain, changeMainStatus } from "@/api/finFilling/main";
import { useRouter } from 'vue-router';

const { proxy } = getCurrentInstance();
const { filling_check_status } = proxy.useDict("filling_check_status");

const mainList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const router = useRouter();


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    guid: null,
    compname: null,
    compid: null,
    year: null,
    compnamesn: null,
    compsnid: null,
    yearupfileGuid: null,
    yearnowfileGuid: null,
    leader: null,
    compId: null,
    deptId: null,
    progress: null,
    status: null,
    enclosureName: null,
    department: null,
    details: null,
    sort: null,
    enable: null,
    isvalid: null,
    pjxms: null,
    sjjz: null,
    preDeptId: null,
    mainType: null,
    nineTable: null,
    zpbg: null,
    downloadTime: null,
    nineResult: null,
    reportResult: null,
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    guid: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    compname: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询填报管理列表 */
function getList() {
  loading.value = true;
  listMain(queryParams.value).then(response => {
    mainList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}


/** 审核状态修改 */
function handleStatusChange(row) {
  let text = row.status === "2" ? "审核" : "取消审核";
  changeMainStatus(row.id, row.status);
  proxy.$modal.msgSuccess(text + "成功");
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    guid: null,
    compname: null,
    compid: null,
    year: null,
    compnamesn: null,
    compsnid: null,
    yearupfileGuid: null,
    yearnowfileGuid: null,
    leader: null,
    compId: null,
    deptId: null,
    progress: null,
    status: null,
    enclosureName: null,
    department: null,
    remark: null,
    details: null,
    sort: null,
    createTime: null,
    updateTime: null,
    enable: null,
    isvalid: null,
    pjxms: null,
    sjjz: null,
    preDeptId: null,
    mainType: null,
    nineTable: null,
    zpbg: null,
    downloadTime: null,
    nineResult: null,
    reportResult: null,
  };
  proxy.resetForm("mainRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据  */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 批量下载公开报告按钮操作 */
function batDownload() {
  const tbNames = row.tableName || tableNames.value;
  if (tbNames == "") {
    proxy.$modal.msgError("请选择要下载的数据");
    return;
  }
  if (row.genType === "1") {
    genCode(row.tableName).then(response => {
      proxy.$modal.msgSuccess("成功生成到自定义路径：" + row.genPath);
    });
  } else {
    proxy.$download.zip("/finFilling/main/batDownload?tables=" + tbNames, ".zip");
  }
}

/** 敏感词按钮操作 */
function sensitiveWord(row) {
  reset();
  const _id = row.id || ids.value;
  getMain(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "敏感词";
  });
}

/** 下载填报链接按钮操作 */
function downLink(row) {
  reset();
  const _id = row.id || ids.value;
  getMain(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "下载填报链接";
  });
}

/** 一键审核按钮操作 */
function oneClickReview(row) {
  reset();
  const _id = row.id || ids.value;
  getMain(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "一键审核";
  });
}

/** 一键取消审核按钮操作 */
function oneClickCancelReview(row) {
  reset();
  const _id = row.id || ids.value;
  getMain(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "一键取消审核";
  });
}

/** 下载绩效上传情况按钮操作 */
function downPrefCondition(row) {
  reset();
  const _id = row.id || ids.value;
  getMain(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "下载绩效上传情况";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["mainRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMain(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMain(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 查看按钮操作 */
function editMain(row) {
  const mainId = row.id || 0;
  const mainName = row.compname;
  router.push('/finFilling/main-editMain/' + mainId)
}




/** 整体绩效表按钮操作 */
function showPref(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除填报管理编号为"' + _ids + '"的数据项？').then(function() {
    return showPref(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 下载公开报告按钮操作 */
function downZip(row) {
  proxy.download('finFilling/main/downZip', {
    ...queryParams.value
  }, `main_${new Date().getTime()}.xlsx`);
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('finFilling/main/export', {
    ...queryParams.value
  }, `main_${new Date().getTime()}.xlsx`);
}

/** 是否渲染字段 */
function renderField(insert, edit) {
  return form.value.id == null ? insert : edit;
}

getList();
</script>